package com.villaday.idf.fragment2;


import android.graphics.Typeface;
import android.os.Bundle;
import com.google.android.material.appbar.AppBarLayout;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;import androidx.fragment.app.FragmentTransaction;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.villaday.idf.R;
import com.villaday.idf.activity.BackHandlerHelper;
import com.villaday.idf.activity.FragmentBackHandler;
import com.villaday.idf.base.BaseFragment;
import com.villaday.idf.fragment3.FindHouseInneJiuDianFragment;
import com.villaday.idf.model2.Modules_find_house_configurations;
import com.villaday.idf.util.Constant;
import com.villaday.idf.util.LogeUtil;
import com.villaday.idf.util.OkHttpUtil;
import com.villaday.idf.util.StatusBarUtil;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * A simple {@link Fragment} subclass.
 */
public class FindHouse2Fragment extends BaseFragment implements OkHttpUtil.OnDataListener, FindHouseInneJiuDianFragment.ImpShowRealRoot, FragmentBackHandler {


   /* @BindView(R.id.tab)
    TabLayout tabLayout;
    @BindView(R.id.vp)
    ViewPager vp;*/

    @BindView(R.id.tv_1)
    TextView tv_1;
    @BindView(R.id.tv_2)
    TextView tv_2;
    @BindView(R.id.tv_3)
    TextView tv_3;
    @BindView(R.id.tv_4)
    TextView tv_4;

    @BindView(R.id.view1)
    View view1;
    @BindView(R.id.view2)
    View view2;
    @BindView(R.id.view3)
    View view3;
    @BindView(R.id.view4)
    View view4;
    @BindView(R.id.ll_1)
    LinearLayout ll_1;
    @BindView(R.id.ll_2)
    LinearLayout ll_2;
    @BindView(R.id.ll_3)
    LinearLayout ll_3;
    @BindView(R.id.ll_4)
    LinearLayout ll_4;

    @BindView(R.id.llroot)
    LinearLayout llroot;
    @BindView(R.id.ll_show)
    LinearLayout appbar_show;
    @BindView(R.id.ll_hint)
    LinearLayout appbar_hint;

    @BindView(R.id.appbar_show)
    AppBarLayout appBarLayout;
    @BindView(R.id.toolbar_back)
    ImageButton toolbar_back;





   /* @BindView(R.id.tv_date)
    TextView tvDate;*/


    private String[] titles = {"别墅", "民宿", "酒店", "度假村"};
    private int page;
    private FindHouseInnerFragment bieshuFragment;
    private FragmentManager fragmentManager;
    private FindHouseInnerMinSuFragment findHouseInnerMinSuFragment;

    private Gson gson = new GsonBuilder().create();
    private FindHouseInneJiuDianFragment findHouseInneJiuDianFragment;

    public FindHouse2Fragment() {
        // Required empty public constructor
    }

    public static FindHouse2Fragment instance(int bournFragmentJumpType) {
        FindHouse2Fragment findHouse2Fragment = new FindHouse2Fragment();

        Bundle bundle = new Bundle();
        bundle.putInt("bournFragmentJumpType", bournFragmentJumpType);
        findHouse2Fragment.setArguments(bundle);
        return findHouse2Fragment;
    }

    public static FindHouse2Fragment instance(int bournFragmentJumpType,boolean isShowBack) {
        FindHouse2Fragment findHouse2Fragment = new FindHouse2Fragment();

        Bundle bundle = new Bundle();
        bundle.putInt("bournFragmentJumpType", bournFragmentJumpType);

        bundle.putBoolean("isShowBack", isShowBack);


        findHouse2Fragment.setArguments(bundle);
        return findHouse2Fragment;
    }

    @Override
    protected int getViewResId() {
        return R.layout.fragment_find_house;
    }

    @Override
    protected void init(View v) {


        StatusBarUtil.setTranslucentForImageViewInFragment(getActivity(), null);
        StatusBarUtil.setLightMode(getActivity());
        //初始化rv

      /*  vp.setAdapter(new FragmentPagerAdapter(getChildFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return BournListFragment.instance();
            }

            @Override
            public int getCount() {
                return titles.length;
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return titles[position];
            }
        });
        tabLayout.setupWithViewPager(vp);

        initEvent();
        initClick();*/

        int bournFragmentJumpType1 = getArguments().getInt("bournFragmentJumpType");

       boolean isShowBack = getArguments().getBoolean("isShowBack",true);
       if (!isShowBack){
           toolbar_back.setVisibility(View.GONE);
       }else {
           toolbar_back.setVisibility(View.VISIBLE);

       }



        initTitle();

        jump(bournFragmentJumpType1);

        appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int i) {
                mRxManager.post("JDMSTMAppBarLayout", i);
            }
        });

    }

    @Override
    protected void loadData() {
        OkHttpUtil.getJSON(getContext(), Constant.URL.modules_find_house_configurations, this);
    }

    public void jump(int type) {//酒店
        if (type == 0) {
            ll_3.performClick();


        } else if (type == 1) {//民宿
            ll_2.performClick();


        } else if (type == 2) {//别墅

            ll_1.performClick();

        }

    }


    private void initTitleSelect(TextView view) {
        //选中后字体变大
        view.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimensionPixelSize(R.dimen.tab_tV_size_big));
        //加粗
        view.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
    }

    private void initTitle() {
        tv_1.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimensionPixelSize(R.dimen.tab_tV_size));
        //取消加粗
        tv_1.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
        tv_2.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimensionPixelSize(R.dimen.tab_tV_size));
        //取消加粗
        tv_2.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
        tv_3.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimensionPixelSize(R.dimen.tab_tV_size));
        //取消加粗
        tv_3.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
        tv_4.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimensionPixelSize(R.dimen.tab_tV_size));
        //取消加粗
        tv_4.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));

        view1.setSelected(false);
        view2.setSelected(false);
        view3.setSelected(false);
        view4.setSelected(false);
    }



    /*  *//**
     * 设置tableLayout的点击监听事件
     *//*
    private void initClick() {

        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                //定义方法，判断是否选中
                updateTabView(tab, true);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                //定义方法，判断是否选中
                updateTabView(tab, false);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
            }
        });
    }

    *//**
     * 用来改变tabLayout选中后的字体大小及颜色
     *
     * @param tab
     * @param isSelect
     *//*
    private void updateTabView(TabLayout.Tab tab, boolean isSelect) {
        //找到自定义视图的控件ID
        TextView tv_tab = (TextView) tab.getCustomView().findViewById(R.id.tv_tab);
        if (isSelect) {
            //设置标签选中
            tv_tab.setSelected(true);
            //选中后字体变大
            tv_tab.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimensionPixelSize(R.dimen.tab_tV_size_big));
            //加粗
            tv_tab.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));


        } else {
            //设置标签取消选中
            tv_tab.setSelected(false);
            //恢复为默认字体大小
            tv_tab.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimensionPixelSize(R.dimen.tab_tV_size));
            //取消加粗
            tv_tab.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
        }
    }


    */

    /**
     * 设置Viewpager的适配器
     *//*
    private void initEvent() {
        //设置Viewpager的适配器
        //用来循环适配器中的视图总数
        for (int i = 0; i < titles.length; i++) {
            //获取每一个tab对象
            TabLayout.Tab tabAt = tabLayout.getTabAt(i);
            //将每一个条目设置我们自定义的视图
            tabAt.setCustomView(R.layout.mytablayout_tv);
            //默认选中第一个
            if (i == 0) {
                // 设置第一个tab的TextView是被选择的样式
                tabAt.getCustomView().findViewById(R.id.tv_tab).setSelected(true);//第一个tab被选中
                //设置选中标签的文字大小
                ((TextView) tabAt.getCustomView().findViewById(R.id.tv_tab)).setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimensionPixelSize(R.dimen.tab_tV_size_big));
            }
            //通过tab对象找到自定义视图的ID
            TextView textView = (TextView) tabAt.getCustomView().findViewById(R.id.tv_tab);
            textView.setText(titles[i]);//设置tab上的文字
        }
    }*/
    public void onRefreshState() {
        StatusBarUtil.setTranslucentForImageViewInFragment(getActivity(), null);
        StatusBarUtil.setLightMode(getActivity());

    }


    public void setCity() {

    }


    @OnClick({R.id.ll_1, R.id.ll_2, R.id.ll_3,/* R.id.tv_4*/})
    public void onViewClicked(View view) {
        if (fragmentManager == null) {

            fragmentManager = getChildFragmentManager();

        }
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        hideFragments(transaction);

        switch (view.getId()) {
            case R.id.ll_1: {

                initTitle();
                initTitleSelect(tv_1);
                view1.setSelected(true);
                page = 0;
                // bieshuFragment = null;
                if (bieshuFragment == null) {
                    bieshuFragment = FindHouseInnerFragment.instance();
                    transaction.add(R.id.fl, bieshuFragment, "0");
                } else {

                    transaction.show(bieshuFragment);
                }
                if (findHouseInnerMinSuFragment != null) {
                    findHouseInnerMinSuFragment.diasspopu();

                }
                if (findHouseInneJiuDianFragment != null) {
                    findHouseInneJiuDianFragment.diasspopu();
                }
                bieshuFragment.diasspopu();
            }
            break;
            case R.id.ll_2: {

                initTitle();
                initTitleSelect(tv_2);
                view2.setSelected(true);
                page = 1;
                //  findHouseInnerMinSuFragment = null;
                if (findHouseInnerMinSuFragment == null) {
                    findHouseInnerMinSuFragment = FindHouseInnerMinSuFragment.instance();
                    transaction.add(R.id.fl, findHouseInnerMinSuFragment, "1");
                } else {
                    transaction.show(findHouseInnerMinSuFragment);
                }
                if (bieshuFragment != null) {
                    bieshuFragment.diasspopu();
                }
                if (findHouseInneJiuDianFragment != null) {
                    findHouseInneJiuDianFragment.diasspopu();
                }
                findHouseInnerMinSuFragment.diasspopu();
            }
            break;
            case R.id.ll_3:

                initTitle();
                initTitleSelect(tv_3);
                view3.setSelected(true);
                page = 2;
                if (findHouseInneJiuDianFragment == null) {
                    findHouseInneJiuDianFragment = FindHouseInneJiuDianFragment.instance();
                    transaction.add(R.id.fl, findHouseInneJiuDianFragment, "2");
                    findHouseInneJiuDianFragment.setImpShowRealRoot(this);

                } else {
                    transaction.show(findHouseInneJiuDianFragment);
                }
                if (bieshuFragment != null) {
                    bieshuFragment.diasspopu();
                }
                if (findHouseInnerMinSuFragment != null) {
                    findHouseInnerMinSuFragment.diasspopu();
                }

                findHouseInneJiuDianFragment.diasspopu();


                break;
            case R.id.tv_4:

                initTitle();
                initTitleSelect(tv_4);
                view4.setSelected(true);
                break;


        }
        transaction.commitAllowingStateLoss();

    }

   /* private void createCustomDatePicker(View view, final TextView result) {
        new DatePopupWindow
                .Builder(getActivity(), Calendar.getInstance().getTime(), view)
                //.setInitSelect(startGroup, startChild, endGroup, endChild)

                .setInitSelect(-1, -1, -1, -1)

                .setInitDay(false)
                .setDateOnClickListener(new DatePopupWindow.DateOnClickListener() {
                    @Override
                    public void getDate(String startDate, String endDate, int startGroupPosition, int startChildPosition, int endGroupPosition, int endChildPosition) {
                        startGroup = startGroupPosition;
                        startChild = startChildPosition;
                        endGroup = endGroupPosition;
                        endChild = endChildPosition;
                        String mStartTime = CalendarUtil.FormatDate_MD(startDate);
                        String mEndTime = CalendarUtil.FormatDate_MD(endDate);
                        result.setText("住" + mStartTime +"\n"+ "离" + mEndTime);
                    }
                }).builder();
    }*/


    /**
     * 先隐藏所有Fragment
     */
    private void hideFragments(FragmentTransaction transaction) {
        if (bieshuFragment != null) {
            transaction.hide(bieshuFragment);
        }

        if (findHouseInnerMinSuFragment != null) {
            transaction.hide(findHouseInnerMinSuFragment);
        }

        if (findHouseInneJiuDianFragment != null) {
            transaction.hide(findHouseInneJiuDianFragment);
        }


    }

    @Override
    public void onResponse(String url, String json) {
        if (!TextUtils.isEmpty(json)) {
            if (Constant.URL.modules_find_house_configurations.equals(url)) {
                LogeUtil.e("modules_find_house_configurations", json);
                Modules_find_house_configurations modules_find_house_configurations = gson.fromJson(json, Modules_find_house_configurations.class);

            }
        }
    }

    @Override
    public void onFailure(String url, String error) {

    }

    @Override
    public void ShowRealRoot() {
        appbar_show.setVisibility(View.VISIBLE);
        appbar_hint.setVisibility(View.GONE);

    }


    @OnClick(R.id.toolbar_back)
    public void onViewClicked() {
        getActivity().onBackPressed();
    }


    @Override
    public boolean onBackPressed() {
        return BackHandlerHelper.handleBackPress(this);
    }
}
